package edu.ccut.saturn.basecomponent.database.facade;

import java.sql.Connection;
import java.util.List;

import edu.ccut.saturn.basecomponent.database.ISelectSQLBuilder;
import edu.ccut.saturn.basecomponent.database.ISaturnSQL;
import edu.ccut.saturn.basecomponent.database.ISaturnSQLBuilder;
import edu.ccut.saturn.basecomponent.database.SaturnSQLBuilderFactory;
import edu.ccut.saturn.basecomponent.database.SaturnSQLHandler;
import edu.ccut.saturn.basecomponent.database.util.SaturnDataUtil;
import edu.ccut.saturn.component.SaturnData;

public class QueryCountByCondition {
	public int execute(String fullKey ,String condition,Connection conn) throws Exception {
		if (condition == null) {
			condition = "";
		}
		condition = " WHERE 1=1 " + condition;
		String dbType = SaturnDataUtil.getStandardDBType(fullKey);
		ISaturnSQLBuilder builder = SaturnSQLBuilderFactory
				.getSaturnSQLBuilder(dbType);
		if (builder != null) {
			ISelectSQLBuilder selectSQLBuilder = builder
					.getSelectSQLBuilder(ISaturnSQL.SELECT_COUNT_OPERATION);
			ISaturnSQL saturnSQL = selectSQLBuilder.build2(fullKey, condition);
			List<SaturnData> list = SaturnSQLHandler.executeQuery(saturnSQL,conn);
			int count = (Integer) list.get(0).get("count");
			return count;
		}
		return 0;
	}
}
